const electron = require('electron');
const Store = require('electron-store');

const store = new Store();
const ipcMain = electron.ipcMain;
const head = '163/';
const neteaseCloudMusicApi = require('NeteaseCloudMusicApi');
module.exports = class netease {
    cookie = '';
    constructor() {
        this.retryGetCookie();
    }
    retryGetCookie(){
        if(this.cookie !== '' && this.cookie !== undefined && this.cookie.length > 4){
            return
        }
        let cookie_get = store.get('163cookie');
        if(cookie_get !== undefined && cookie_get !== ''){
            this.cookie = cookie_get;
        }
    }
    init() {

        ipcMain.handle(head+'top_playlist', async (event, args) => {
            this.retryGetCookie();
            return await neteaseCloudMusicApi.top_playlist({...args,cookie:this.cookie})

        })
        ipcMain.handle(head+'login_status', async (event, args) => {
            this.retryGetCookie();
            return await neteaseCloudMusicApi.login_status({...args,cookie:this.cookie})
        })
        ipcMain.handle(head+'login_cellphone', async (event, args) => {
            this.retryGetCookie();
            return await neteaseCloudMusicApi.login_cellphone({...args,cookie:this.cookie})
        })
        ipcMain.handle(head+'playlist_detail', async (event, args) => {
            this.retryGetCookie();
            return await neteaseCloudMusicApi.playlist_detail({...args,cookie:this.cookie})
        })
        ipcMain.handle(head+'song_detail', async (event, args) => {
            this.retryGetCookie();
            return await neteaseCloudMusicApi.song_detail({...args,cookie:this.cookie})
        })
        ipcMain.handle(head+'cloudsearch', async (event, args) => {
            this.retryGetCookie();
            return await neteaseCloudMusicApi.cloudsearch({...args,cookie:this.cookie})
        })
        ipcMain.handle(head+'album', async (event, args) => {
            this.retryGetCookie();
            return await neteaseCloudMusicApi.album({...args,cookie:this.cookie})
        })
        ipcMain.handle(head+'playlist_catlist', async (event, args) => {
            this.retryGetCookie();
            return await neteaseCloudMusicApi.playlist_catlist({...args,cookie:this.cookie})
        })
        ipcMain.handle(head+'toplist', async (event, args) => {
            this.retryGetCookie();
            return await neteaseCloudMusicApi.toplist({...args,cookie:this.cookie})
        })
        ipcMain.handle(head+'artist_list', async (event, args) => {
            this.retryGetCookie();
            return await neteaseCloudMusicApi.artist_list({...args,cookie:this.cookie})
        })
        ipcMain.handle(head+'lyric', async (event, args) => {
            this.retryGetCookie();
            return await neteaseCloudMusicApi.lyric({...args,cookie:this.cookie})
        })
        ipcMain.handle(head+'artist_album', async (event, args) => {
            this.retryGetCookie();
            return await neteaseCloudMusicApi.artist_album({...args,cookie:this.cookie})
        })

    }

}
